﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using MoneyWentWhere.WinFormsUI.Properties;
using MoneyWentWhere.DataModel;
using MoneyWentWhere.DataModel.Storage;
using System.Windows.Forms;

namespace MoneyWentWhere.WinFormsUI
{
    public class FileManager
    {
        MoneyDataSet _dataSet;

        public MoneyDataSet DataSet
        {
            get
            {
                if (_dataSet == null)
                {
                    _dataSet = GetDefaultDataSet();
                }
                return _dataSet;
            }
            set
            {
                _dataSet = value;
            }
        }

        MoneyDataSet GetDefaultDataSet()
        {
            MoneyDataSet ds = null;
            bool loaded = false;
            if (File.Exists(StartupFile))
            {
                loaded = TryOpenFile(StartupFile, out ds);
            }

            if (!loaded)
            {
                StartupFile = CreateNewFileName(StartupFile);
                ds = new MoneyDataSet(StartupFile, DataFormatX.Excel2007);
            }
            return ds;
        }

        public bool TryOpenFile(String filename, out MoneyDataSet ds)
        {
            ds = null; 
            String password = null;
            while (true)
            {
                try
                {
                    ds = MoneyDataSet.Load(filename, password);
                    break;
                }
                catch (InvalidPasswordException)
                {
                    // Ask for password
                    password = InputBox.GetText(
                        Resources.enterPasswordTitle,
                        String.Format(Resources.enterPasswordPrompt, filename));

                    if (password == null) { return false; }
                }
                catch (Exception ex2)
                {
                    MessageBox.Show("Cannot load file.\r\n" + ex2.Message);
                    return false;
                }
            }

            return true;
        }

        public String StartupFile
        {
            get
            {
                if (String.IsNullOrEmpty(Settings.Default.StartupFile))
                {
                    Settings.Default.StartupFile = NewFile;
                    Settings.Default.Save();
                }
                return Settings.Default.StartupFile;
            }
            set
            {
                Settings.Default.StartupFile = value;
                Settings.Default.Save();
            }
        }

        public String NewFile
        {
            get
            {
                return Path.Combine(
                      Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
                      "MoneyWentWhere.xlsx");
            }

        }

        public static string CreateNewFileName(String proposedName)
        {
            String ext = Path.GetExtension(proposedName);
            String nameNoExt = proposedName.Substring(0, proposedName.Length - ext.Length);

            int count = 1;
            String name = proposedName;
            while (File.Exists(name))
            {
                name = String.Format("{0}{1}{2}", nameNoExt, count++, ext);
            }
            return name;
        }


    }
}
